# _*_ coding: utf-8 _*_
# .@FileName:attr_regex_match_dialog
# .@Data....:2025-02-04 : 17 : 38
# .@Aurhor..:LiuJingYu
"""
launch:
   import maya.cmds as cmds
   FileName.main() 
"""
from __future__ import unicode_literals, print_function

from PySide2.QtWidgets import *
from PySide2.QtGui import *
import attr_regex_match

import setting

class RegexMatchDialog(QDialog):
    def __init__(self, parent=None):
        # 调用父类QDialog的构造函数，确保父类的初始化逻辑被执行
        super(RegexMatchDialog, self).__init__(parent)

        self.names = None
        self.setWindowTitle("regex match")
        self.main_layout = QHBoxLayout(self)

        self.exp_line_edit = QLineEdit()
        self.match_bn = QPushButton("匹配")
        self.match_bn.clicked.connect(self.gen)

        self.main_layout.addWidget(QLabel("正则表达式： "))
        self.main_layout.addWidget(self.exp_line_edit)
        self.main_layout.addWidget(self.match_bn)

        self.setting_font()

    def setting_font(self):
        fs = setting.get("font", None)
        if fs is None:
            fs =  QFont().toString()
        f = QFont()
        f.fromString(fs)
        self.setFont(f)

    def gen(self):
        try:
            # 获取用户输入的正则表达式
            regex_pattern = self.exp_line_edit.text()
            # 调用 regex_match 模块的 match 函数进行匹配
            self.names = list(attr_regex_match.match(regex_pattern))
            if self.names:
                # 若有匹配结果，使用消息框显示结果
                result_str = "\n".join(self.names)
                QMessageBox.information(self, "匹配结果", f"匹配到的名称如下：\n{result_str}")
            else:
                # 若无匹配结果，提示用户
                QMessageBox.information(self, "匹配结果", "未匹配到任何名称。")
        except Exception as e:
            # 捕获并处理异常，给用户友好的提示
            QMessageBox.critical(self, "错误", f"匹配过程中出现错误：{str(e)}")
        finally:
            # 无论是否有异常，最后关闭对话框
            self.close()


def exec_(parent=None):
    dia = RegexMatchDialog(parent)
    dia.exec_()
    return dia.names


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    exec_()
    sys.exit(app.exec_())